{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sequential Search\n",
    "\n",
    "Check out the video lecture for a full breakdown, in this Notebook all we do is implement Sequential Search for an Unordered List and an Ordered List."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "____\n",
    "## Sequential Search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def seq_search(arr,ele):\n",
    "    \"\"\"\n",
    "    General Sequential Search. Works on Unordered lists.\n",
    "    \"\"\"\n",
    "    \n",
    "    # Start at position 0\n",
    "    pos = 0\n",
    "    # Target becomes true if ele is in the list\n",
    "    found = False\n",
    "    \n",
    "    # go until end of list\n",
    "    while pos < len(arr) and not found:\n",
    "        \n",
    "        # If match\n",
    "        if arr[pos] == ele:\n",
    "            found = True\n",
    "            \n",
    "        # Else move one down\n",
    "        else:\n",
    "            pos  = pos+1\n",
    "    \n",
    "    return found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "arr = [1,9,2,8,3,4,7,5,6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print seq_search(arr,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "print seq_search(arr,10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ordered List\n",
    "\n",
    "If we know the list is ordered than, we only have to check until we have found the element or an element greater than it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def ordered_seq_search(arr,ele):\n",
    "    \"\"\"\n",
    "    Sequential search for an Ordered list\n",
    "    \"\"\"\n",
    "    # Start at position 0\n",
    "    pos = 0\n",
    "    \n",
    "    # Target becomes true if ele is in the list\n",
    "    found = False\n",
    "    \n",
    "    # Stop marker\n",
    "    stopped = False\n",
    "    \n",
    "    # go until end of list\n",
    "    while pos < len(arr) and not found and not stopped:\n",
    "        \n",
    "        # If match\n",
    "        if arr[pos] == ele:\n",
    "            found = True\n",
    "            \n",
    "        else:\n",
    "            \n",
    "            # Check if element is greater\n",
    "            if arr[pos] > ele:\n",
    "                stopped = True\n",
    "                \n",
    "            # Otherwise move on\n",
    "            else:\n",
    "                pos  = pos+1\n",
    "    \n",
    "    return found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "arr.sort() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ordered_seq_search(arr,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ordered_seq_search(arr,1.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Good Job!"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
